<script>
import { getRoleByIdAjax, saveOrUpdate } from '@/data/RoleData'

const formData = {}
export default {
  name: 'RoleEdit',
  data () {
    return {
      show: false,
      role: { ...formData },
      rules: {
        name: [
          { required: true, message: '角色名不能为空' }
        ],
        status: [
          { required: true, message: '状态不能为空' }
        ]
      }
    }
  },
  methods: {
    getRole (id) {
      this.role = { ...formData }
      this.show = true
      if (id) {
        const res = getRoleByIdAjax(id)
        if (res.code === 200) {
          this.role = { ...res.data }
        } else {
          this.$alert(res.msg, { type: 'error' })
        }
      }
    },
    submit () {
      this.$refs.formData.validate(valid => {
        if (valid) {
          const res = saveOrUpdate(this.role)
          if (res.code === 200) {
            this.$message.success(`${this.role.id ? '修改' : '添加'}成功`)
            this.show = false
            this.$emit('close')
          } else {
            this.$alert(res.msg, { type: 'error' })
          }
        }
      })
    }
  }
}
</script>

<template>
<el-dialog v-if="show" :visible.sync="show" title="角色编辑页">
<el-form ref="formData" :model="role" :rules="rules" label-width="80px">
  <el-form-item label="角色名" prop="name">
    <el-input v-model="role.name" placeholder="请输入角色名"/>
  </el-form-item>
  <el-form-item label="介绍">
    <el-input type="textarea" :rows="4" maxlength="150" show-word-limit v-model="role.profile" placeholder="请输入介绍内容"/>
  </el-form-item>
  <el-form-item label="状态" prop="status">
    <el-radio-group v-model="role.status">
      <el-radio :label="1">可用</el-radio>
      <el-radio :label="2">禁用</el-radio>
    </el-radio-group>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submit">提交</el-button>
  </el-form-item>
</el-form>
</el-dialog>
</template>

<style scoped>

</style>
